<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

class Adapay
{
    protected $appid;
    protected $draw_cash_type;
    protected $url;
    protected static $uniacid;
    public function __construct($uniacid)
    {
        self::$uniacid = $uniacid;
        $config_model = new \app\adapay\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $this->appid = $config["appid"];
        $this->draw_cash_type = $config["draw_cash_type"];
        include_once EXTEND_PATH . "adapay/AdapaySdk/init.php";
        include_once EXTEND_PATH . "adapay/AdapayDemo/config.php";
        $this->url = getConfigSetting($uniacid, "callback_url");
        $this->url = !empty($this->url) ? $this->url : "https://" . $_SERVER["HTTP_HOST"];
    }
    public function createPay($order_code, $pay_price, $opend_id, $pay_type = "wx_pub", $pay_mode = "delay")
    {
        include_once EXTEND_PATH . "adapay/AdapaySdk/init.php";
        include_once EXTEND_PATH . "adapay/AdapayDemo/config.php";
        $account = new \AdaPaySdk\Payment();
        $obj_params = ["app_id" => $this->appid, "order_no" => $order_code, "pay_channel" => $pay_type, "pay_amt" => sprintf("%01.2f", $pay_price), "goods_title" => "服务", "goods_desc" => "服务", "description" => "description", "device_info" => ["device_p" => "111.121.9.10"], "expend" => ["open_id" => $opend_id], "notify_url" => $this->url . "/adapay/CallBack/payCallback"];
        if(!empty($pay_mode)) {
            $obj_params["pay_mode"] = $pay_mode;
        }
        $account->create($obj_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function createAliPay($order_code, $pay_price, $pay_type = "wx_pub", $pay_mode = "delay")
    {
        include_once EXTEND_PATH . "adapay/AdapaySdk/init.php";
        include_once EXTEND_PATH . "adapay/AdapayDemo/config.php";
        $account = new \AdaPaySdk\Payment();
        $obj_params = ["app_id" => $this->appid, "order_no" => $order_code, "pay_channel" => $pay_type, "pay_amt" => sprintf("%01.2f", $pay_price), "goods_title" => "服务", "goods_desc" => "服务", "description" => "description", "device_info" => ["device_p" => "111.121.9.10"], "expend" => [], "notify_url" => $this->url . "/adapay/CallBack/payCallback"];
        if(!empty($pay_mode)) {
            $obj_params["pay_mode"] = $pay_mode;
        }
        $account->create($obj_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function orderInfo($payment_id)
    {
        $account = new \AdaPaySdk\Payment();
        $obj_params = ["payment_id" => $payment_id];
        $account->query($obj_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function confirmorderCreate($payment_id, $order_code, $confirm_amt, $div_members, $text = "", $fee_mode = 1)
    {
        $fee_mode = $fee_mode == 1 ? "I" : "O";
        $payment = new \AdaPaySdk\PaymentConfirm();
        $payment_params = ["payment_id" => $payment_id, "order_no" => $order_code, "confirm_amt" => sprintf("%01.2f", $confirm_amt), "description" => $text, "div_members" => $div_members, "fee_mode" => $fee_mode];
        $payment->create($payment_params);
        $res = $payment->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function orderRefund($payment_id, $order_code, $price, $action = "refundCallback")
    {
        $payment = new \AdaPaySdk\PaymentReverse();
        $payment_params = ["payment_id" => $payment_id, "app_id" => $this->appid, "order_no" => $order_code, "reverse_amt" => sprintf("%01.2f", $price), "notify_url" => $this->url . "/adapay/CallBack/" . $action];
        $payment->create($payment_params);
        $res = $payment->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function orderRefundNoAda($payment_id, $order_code, $price, $action = "refundCallback")
    {
        $payment = new \AdaPaySdk\refund();
        $payment_params = ["payment_id" => $payment_id, "refund_order_no" => $order_code, "refund_amt" => sprintf("%01.2f", $price), "notify_url" => $this->url . "/adapay/CallBack/" . $action];
        $payment->create($payment_params);
        $res = $payment->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function createUserObj($member_id)
    {
        $member = new \AdaPaySdk\Member();
        $member_params = ["app_id" => $this->appid, "member_id" => $member_id];
        $member->create($member_params);
        $res = $member->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function createCompanyObj($data, $type, $http, $bank_info)
    {
        $member = new \AdaPaySdk\CorpMember();
        if(isset($data["attach_file"])) {
            $http = !strstr($data["attach_file"], "https://") ? "" : "https://";
            $data["attach_file"] = realpath(str_replace($http . $_SERVER["HTTP_HOST"] . "/", "", $data["attach_file"]));
        }
        $member_params = ["app_id" => $this->appid, "member_id" => $data["member_id"], "order_no" => $data["order_no"], "name" => $data["name"], "prov_code" => $data["prov_code"], "area_code" => $data["area_code"], "social_credit_code" => $data["social_credit_code"], "social_credit_code_expires" => $data["social_credit_code_expires"], "business_scope" => $data["business_scope"], "legal_person" => $data["legal_person"], "legal_cert_id" => $data["legal_cert_id"], "legal_cert_id_expires" => $data["legal_cert_id_expires"], "legal_mp" => $data["legal_mp"], "address" => $data["address"], "attach_file" => new \CURLFile($data["attach_file"]), "notify_url" => $this->url . "/adapay/CallBack/companyCallback", "bank_code" => !empty($bank_info["bank_code"]) ? $bank_info["bank_code"] : "", "bank_acct_type" => !empty($bank_info["bank_acct_type"]) ? $bank_info["bank_acct_type"] : 2, "card_no" => !empty($bank_info["card_id"]) ? $bank_info["card_id"] : "", "card_name" => !empty($bank_info["card_name"]) ? $bank_info["card_name"] : ""];
        if($type == 1) {
            $member->create($member_params);
        } else {
            $member->update($member_params);
        }
        $res = $member->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function createAccountsObj($data)
    {
        $account = new \AdaPaySdk\SettleAccount();
        $account_params = ["app_id" => $this->appid, "member_id" => $data["member_id"], "channel" => "bank_account", "account_info" => ["card_id" => $data["card_id"], "card_name" => $data["card_name"], "tel_no" => $data["tel_no"], "bank_acct_type" => $data["bank_acct_type"]]];
        if($data["bank_acct_type"] == 1) {
            $account_params["account_info"]["bank_code"] = $data["bank_code"];
            $account_params["account_info"]["prov_code"] = $data["prov_code"];
            $account_params["account_info"]["area_code"] = $data["area_code"];
        } else {
            $account_params["account_info"]["cert_id"] = $data["cert_id"];
            $account_params["account_info"]["cert_type"] = "00";
        }
        $account->create($account_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function delAccountsObj($data)
    {
        $account = new \AdaPaySdk\SettleAccount();
        $account_params = ["app_id" => $this->appid, "member_id" => $data["member_id"], "settle_account_id" => $data["settle_account_id"]];
        $account->delete($account_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function drawCash($order_code, $member_id, $cash, $uniacid = 666)
    {
        $drawcash = new \AdaPaySdk\Drawcash();
        $drawcash_params = ["order_no" => $order_code, "app_id" => $this->appid, "cash_type" => $this->draw_cash_type, "cash_amt" => sprintf("%01.2f", $cash), "member_id" => $member_id, "notify_url" => $this->url . "/adapay/CallBack/walletCallback"];
        $drawcash->create($drawcash_params);
        $res = $drawcash->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function drawCashInfo($order_code)
    {
        $drawcash = new \AdaPaySdk\Drawcash();
        $cash_params = ["order_no" => $order_code];
        $drawcash->query($cash_params);
        $res = $drawcash->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function settleAccount($member_id, $adapay_id)
    {
        $account = new \AdaPaySdk\SettleAccount();
        $account_params = ["app_id" => $this->appid, "member_id" => $member_id];
        if(!empty($adapay_id)) {
            $account_params["settle_account_id"] = $adapay_id;
        }
        $account->balance($account_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function balancePay($member_id, $cash)
    {
        $account = new \AdaPaySdk\AdaPayCommon();
        $account_params = ["adapay_func_code" => "settle_accounts.balancePay", "app_id" => $this->appid, "out_member_id" => 0, "in_member_id" => $member_id, "order_no" => orderCode(), "trans_amt" => sprintf("%01.2f", $cash), "goods_title" => "balancePay", "goods_desc" => "balancePay"];
        $account->requestAdapay($account_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
    public function settleDetails($member_id, $settle_account_id)
    {
        $account = new \AdaPaySdk\SettleAccount();
        $account_params = ["app_id" => $this->appid, "member_id" => $member_id, "settle_account_id" => $settle_account_id, "begin_date" => date("Ymd", time() - 86400), "end_date" => date("Ymd", time())];
        $account->query($account_params);
        $res = $account->result;
        $data = json_decode($res[1], true);
        $data = json_decode($data["data"], true);
        return $data;
    }
}

?>